package com.atchangsha.system.service.impl;

import com.atchangsha.system.common.utils.Result;
import com.atchangsha.system.constant.WorkerConstants;
import com.atchangsha.system.dto.WorkerDTO;
import com.atchangsha.system.dto.WorkerLoginDto;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atchangsha.system.common.utils.PageUtils;
import com.atchangsha.system.common.utils.Query;
import com.atchangsha.system.dao.WorkerDao;
import com.atchangsha.system.entity.WorkerEntity;
import com.atchangsha.system.service.WorkerService;

@Service("workerService")
public class WorkerServiceImpl extends ServiceImpl<WorkerDao, WorkerEntity> implements WorkerService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<WorkerEntity> wrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        if (key != null && !"".equals(key.trim())) {
            wrapper.like("name", key).or().like("username", key);
        }
        IPage<WorkerEntity> page = this.page(
                new Query<WorkerEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public Result registry(WorkerLoginDto workerLoginDto) {
        String username = workerLoginDto.getUsername();
        WorkerEntity one = baseMapper.selectOne(new QueryWrapper<WorkerEntity>().eq("username", username));
        if (one != null) {
            return Result.fail(WorkerConstants.WorkerEnum.REPEAT);
        }

        WorkerEntity workerEntity = new WorkerEntity();
        workerEntity.setUsername(workerLoginDto.getUsername());
        workerEntity.setPassword(workerLoginDto.getPassword());
        workerEntity.setName(workerLoginDto.getName());
        workerEntity.setPhone(workerLoginDto.getPhone());
        workerEntity.setIdentity("工作人员");

        int insert = baseMapper.insert(workerEntity);
        if (insert == 1) {
            return Result.ok(WorkerConstants.WorkerEnum.REGISTRY_SUCCESS);
        }

        return Result.fail(WorkerConstants.WorkerEnum.REGISTRY_FAIL);
    }

    @Override
    public Result login(String username, String password) {
        WorkerEntity entity = baseMapper.selectOne(new QueryWrapper<WorkerEntity>().eq("username", username));
        if (entity == null) {
            return Result.fail(WorkerConstants.WorkerEnum.USERNAME_FAIL);
        }

        if (!entity.getPassword().equals(password)) {
            return Result.fail(WorkerConstants.WorkerEnum.PASSWORD_FAIL);
        }
        return Result.ok(WorkerConstants.WorkerEnum.LOGIN_SUCCESS).setResult(entity.toEntity());
    }

    @Override
    public Result updateMessage(WorkerDTO workerDTO) {
        WorkerEntity workerEntity = workerDTO.toEntity();
        workerEntity.setUpdateTime(new Date());
        int num = baseMapper.updateById(workerEntity);
        if (num == 1) {
            return Result.ok(WorkerConstants.WorkerEnum.UPDATE_SUCCESS).setResult(workerEntity.toEntity());
        }
        return Result.fail(WorkerConstants.WorkerEnum.UPDATE_FAIL);
    }

}